package subject_set.top101;

/**
 * @author haomin
 * @date 2022/10/27 20:47
 **/
public class BM2 {
    public ListNode reverseBetween (ListNode head, int m, int n) {
        // write code here
        if(head == null || head.next == null) return head;
        ListNode dummyHead = new ListNode(-1), pre, cur, next;
        dummyHead.next = head;
        pre = dummyHead;
        int count1 = m - 1;
        while(count1-- > 0) pre = pre.next;
        cur = pre.next;
        int count2 = n - m;
        while(count2-- > 0) {
            next = cur.next;
            cur.next = next.next;
            next.next = pre.next;
            pre.next = next;
        }
        return dummyHead.next;
    }
}